package com.megvii.koala.tv.presenter;

import com.megvii.corelib.util.AppUtil;
import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.constance.AppUpdateConstance;
import com.megvii.koala.net.NetAPI;
import com.megvii.koala.tv.activity.MainActivity;
import com.megvii.koala.tv.data.ServerManager;
import com.megvii.koala.tv.data.SurveillanceCameraManager;
import com.megvii.koala.tv.data.TVBoxManager;
import com.megvii.koala.tv.data.TVGlobalData;
import com.megvii.koala.tv.fragment.BoxSettingFragment;
import com.megvii.koala.tv.fragment.MenuFragment;
import com.megvii.koala.tv.fragment.RecognizeCamerasSettingFragment;
import com.megvii.koala.tv.fragment.ServerSettingFragment;
import com.megvii.koala.tv.fragment.WebFragment;
import com.megvii.koala.tv.fragment.param.MenuParam;
import com.megvii.koala.tv.fragment.param.ServerSettingParam;
import com.megvii.koala.update.AppUpdateChecker;

/**
 * Created by linyue on 16/1/22.
 */
public class MainPresenter extends KoalaPresenter<MainActivity>
        implements MainActivity.MainActivityListener, AppUpdateChecker.AppUpdateCheckerListener {
    private ServerManager serverManager;

    private TVBoxManager boxManager;

    private SurveillanceCameraManager cameraManager;

    private AppUpdateChecker updateChecker;

    @Override
    protected void onCreated() {
        super.onCreated();
        serverManager = TVGlobalData.instance().serverManager;
        boxManager = TVGlobalData.instance().boxManager;
        cameraManager = TVGlobalData.instance().cameraManager;
        viewHolder.setListener(this);
        updateChecker = new AppUpdateChecker(viewHolder, viewHolder,
                AppUpdateConstance.FILENAME_PREFIX_TV);
        updateChecker.setListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        verifyInitSettings();
        updateBluetoothState();
    }

    @Override
    protected void onDestory() {
        super.onDestory();
        updateChecker.setListener(null);
        updateChecker.release();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //这里应该处理主界面被关闭的事件，有可能程序被杀掉。记录信息
    }

    private void verifyInitSettings() {
        if (!serverManager.setted()) {
            viewHolder.startFragment(ServerSettingFragment.class, new ServerSettingParam(true));
        } else {
            NetAPI.setUrlBase(serverManager.getAddress());
            if (!updateChecker.isPassedChecking() && !updateChecker.isUpdating()) {
                updateChecker
                        .checkUpdate(AppUpdateConstance.TYPE_TV, AppUtil.getAppVersion(viewHolder),
                                false);
            }
            if (updateChecker.isPassedChecking()) {
                if (!boxManager.selected()) {
                    viewHolder.startFragment(BoxSettingFragment.class, null);
                } else {
                    if (!cameraManager.isPersistented()) {
                        viewHolder.startFragment(RecognizeCamerasSettingFragment.class, null);
                    }
                    //Neo 取消每次关闭所有fragment的做法
//                    else
//                    {
//                        viewHolder.finishFragments();
//                    }
                }
            }
        }
    }

    private void updateBluetoothState() {
        if (viewHolder.getFragmentStackSize() > 0) {
            viewHolder.hideError();
        }
    }

    //bluetooth callback start
    @Override
    public void onClickMenuKey() {
        WebFragment webFragment = viewHolder.getWebFragment();
        WebPresenter webPresenter = webFragment.getWebPresenter();
        MenuParam param = new MenuParam(webPresenter.isLocalVideoMode());
        viewHolder.startFragment(MenuFragment.class, param);
    }

    @Override
    public void onFragmentStackChanged() {
        updateBluetoothState();
    }

    @Override
    public void onConfirmedUpdate() {
        verifyInitSettings();
    }

    @Override
    public void onRejectedUpdate() {
        verifyInitSettings();
    }

    @Override
    public void onNoUpdate() {
        verifyInitSettings();
    }
    //update cheker end
}
